﻿using System;
using System.Data;
using System.Data.SQLite;
using System.Text;
using EIM_Accounting.Common.DBUtility;//Please add references

namespace Accounting.DAL
{
    /// <summary>
    /// 数据访问类:AccountingComp
    /// </summary>
    public partial class AccountingComp
    {
        public AccountingComp()
        { }

        #region Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQLite.GetMaxID("ID", "AccountingComp");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from AccountingComp");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,4)
};
            parameters[0].Value = ID;

            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Accounting.Model.AccountingComp model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into AccountingComp(");
            strSql.Append("StaffID,AccountingMonth,Amount,CalcDate,ContractID,LegalDays,FactDays,BaseLine,TotalAmount,WageAmount,CustomerAmount)");
            strSql.Append(" values (");
            strSql.Append("@StaffID,@AccountingMonth,@Amount,@CalcDate,@ContractID,@LegalDays,@FactDays,@BaseLine,@TotalAmount,@WageAmount,@CustomerAmount)");
            strSql.Append(";select LAST_INSERT_ROWID()");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@StaffID", DbType.Int32,8),
					new SQLiteParameter("@AccountingMonth", DbType.String),
					new SQLiteParameter("@Amount", DbType.Decimal,4),
					new SQLiteParameter("@CalcDate", DbType.String),
					new SQLiteParameter("@ContractID", DbType.Int32,8),
					new SQLiteParameter("@LegalDays", DbType.String),
					new SQLiteParameter("@FactDays", DbType.String),
					new SQLiteParameter("@BaseLine", DbType.String),
					new SQLiteParameter("@TotalAmount", DbType.Decimal,4),
					new SQLiteParameter("@WageAmount", DbType.Decimal,4),
					new SQLiteParameter("@CustomerAmount", DbType.Decimal,4)};
            parameters[0].Value = model.StaffID;
            parameters[1].Value = model.AccountingMonth;
            parameters[2].Value = model.Amount;
            parameters[3].Value = model.CalcDate;
            parameters[4].Value = model.ContractID;
            parameters[5].Value = model.LegalDays;
            parameters[6].Value = model.FactDays;
            parameters[7].Value = model.BaseLine;
            parameters[8].Value = model.TotalAmount;
            parameters[9].Value = model.WageAmount;
            parameters[10].Value = model.CustomerAmount;

            object obj = DbHelperSQLite.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Accounting.Model.AccountingComp model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update AccountingComp set ");
            strSql.Append("StaffID=@StaffID,");
            strSql.Append("AccountingMonth=@AccountingMonth,");
            strSql.Append("Amount=@Amount,");
            strSql.Append("CalcDate=@CalcDate,");
            strSql.Append("ContractID=@ContractID,");
            strSql.Append("LegalDays=@LegalDays,");
            strSql.Append("FactDays=@FactDays,");
            strSql.Append("BaseLine=@BaseLine,");
            strSql.Append("TotalAmount=@TotalAmount,");
            strSql.Append("WageAmount=@WageAmount,");
            strSql.Append("CustomerAmount=@CustomerAmount");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@StaffID", DbType.Int32,8),
					new SQLiteParameter("@AccountingMonth", DbType.String),
					new SQLiteParameter("@Amount", DbType.Decimal,4),
					new SQLiteParameter("@CalcDate", DbType.String),
					new SQLiteParameter("@ContractID", DbType.Int32,8),
					new SQLiteParameter("@LegalDays", DbType.String),
					new SQLiteParameter("@FactDays", DbType.String),
					new SQLiteParameter("@BaseLine", DbType.String),
					new SQLiteParameter("@TotalAmount", DbType.Decimal,4),
					new SQLiteParameter("@WageAmount", DbType.Decimal,4),
					new SQLiteParameter("@CustomerAmount", DbType.Decimal,4),
					new SQLiteParameter("@ID", DbType.Int32,8)};
            parameters[0].Value = model.StaffID;
            parameters[1].Value = model.AccountingMonth;
            parameters[2].Value = model.Amount;
            parameters[3].Value = model.CalcDate;
            parameters[4].Value = model.ContractID;
            parameters[5].Value = model.LegalDays;
            parameters[6].Value = model.FactDays;
            parameters[7].Value = model.BaseLine;
            parameters[8].Value = model.TotalAmount;
            parameters[9].Value = model.WageAmount;
            parameters[10].Value = model.CustomerAmount;
            parameters[11].Value = model.ID;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from AccountingComp ");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,4)
};
            parameters[0].Value = ID;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from AccountingComp ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Accounting.Model.AccountingComp GetModel(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,StaffID,AccountingMonth,Amount,CalcDate,ContractID,LegalDays,FactDays,BaseLine,TotalAmount,WageAmount,CustomerAmount from AccountingComp ");
            strSql.Append(" where ID=@ID");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,4)
};
            parameters[0].Value = ID;

            Accounting.Model.AccountingComp model = new Accounting.Model.AccountingComp();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["StaffID"] != null && ds.Tables[0].Rows[0]["StaffID"].ToString() != "")
                {
                    model.StaffID = int.Parse(ds.Tables[0].Rows[0]["StaffID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AccountingMonth"] != null && ds.Tables[0].Rows[0]["AccountingMonth"].ToString() != "")
                {
                    model.AccountingMonth = ds.Tables[0].Rows[0]["AccountingMonth"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Amount"] != null && ds.Tables[0].Rows[0]["Amount"].ToString() != "")
                {
                    model.Amount = decimal.Parse(ds.Tables[0].Rows[0]["Amount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CalcDate"] != null && ds.Tables[0].Rows[0]["CalcDate"].ToString() != "")
                {
                    model.CalcDate = ds.Tables[0].Rows[0]["CalcDate"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ContractID"] != null && ds.Tables[0].Rows[0]["ContractID"].ToString() != "")
                {
                    model.ContractID = int.Parse(ds.Tables[0].Rows[0]["ContractID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LegalDays"] != null && ds.Tables[0].Rows[0]["LegalDays"].ToString() != "")
                {
                    model.LegalDays = ds.Tables[0].Rows[0]["LegalDays"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FactDays"] != null && ds.Tables[0].Rows[0]["FactDays"].ToString() != "")
                {
                    model.FactDays = ds.Tables[0].Rows[0]["FactDays"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BaseLine"] != null && ds.Tables[0].Rows[0]["BaseLine"].ToString() != "")
                {
                    model.BaseLine = ds.Tables[0].Rows[0]["BaseLine"].ToString();
                }
                if (ds.Tables[0].Rows[0]["TotalAmount"] != null && ds.Tables[0].Rows[0]["TotalAmount"].ToString() != "")
                {
                    model.TotalAmount = decimal.Parse(ds.Tables[0].Rows[0]["TotalAmount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["WageAmount"] != null && ds.Tables[0].Rows[0]["WageAmount"].ToString() != "")
                {
                    model.WageAmount = decimal.Parse(ds.Tables[0].Rows[0]["WageAmount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CustomerAmount"] != null && ds.Tables[0].Rows[0]["CustomerAmount"].ToString() != "")
                {
                    model.CustomerAmount = decimal.Parse(ds.Tables[0].Rows[0]["CustomerAmount"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,StaffID,AccountingMonth,Amount,CalcDate,ContractID,LegalDays,FactDays,BaseLine,TotalAmount,WageAmount,CustomerAmount ");
            strSql.Append(" FROM AccountingComp ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQLite.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@tblName", DbType.VarChar, 255),
                    new SQLiteParameter("@fldName", DbType.VarChar, 255),
                    new SQLiteParameter("@PageSize", DbType.Int32),
                    new SQLiteParameter("@PageIndex", DbType.Int32),
                    new SQLiteParameter("@IsReCount", DbType.bit),
                    new SQLiteParameter("@OrderType", DbType.bit),
                    new SQLiteParameter("@strWhere", DbType.VarChar,1000),
                    };
            parameters[0].Value = "AccountingComp";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion Method
    }
}